
package com.probiz.estore.exports.handler.product;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.probiz.estore.common.model.attribute.Attribute;
import com.probiz.estore.common.model.attribute.ProductAttrValue;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductAttGroup;
import com.probiz.estore.common.model.product.ProductAttGroupItem;
import com.probiz.estore.core.util.ExcelUtils;
import com.probiz.estore.exports.handler.ColumnHandler;
import com.probiz.estore.exports.model.Column;


public class AttributeHandler implements ColumnHandler{
	public final static Short FLAG_TRUE = new Short((short) 1);
	public String getProperty(Object target, Column column, HSSFSheet sheet, HSSFRow row, List<String> listHeader) {
		Product product = (Product)target;
		Map<String,String> attrValue = new HashMap<String, String>();
		Set<ProductAttrValue> set_pav = product.getProductAttrValues();
		Iterator<ProductAttrValue> it_pav = set_pav.iterator();
		while(it_pav.hasNext()){
			ProductAttrValue pav = it_pav.next();
			switch(pav.getAttributeDataType()){
				case 1:
					attrValue.put(pav.getAttributeName(), pav.getShortTextValue());
					break;
				case 2:
					attrValue.put(pav.getAttributeName(), pav.getShortTextValue());
					break;
				case 3:
					attrValue.put(pav.getAttributeName(), pav.getShortTextValue());
					break;
				case 4:
					StringBuffer sbInt = new StringBuffer("");
					if(pav.getIntValue() != null){
						sbInt.append(pav.getIntValue().toString());
					}
					attrValue.put(pav.getAttributeName(), sbInt.toString());
					break;
				case 5:
					StringBuffer sbDB = new StringBuffer("");
					if(pav.getDecimalValue() != null){
						sbDB.append(pav.getDecimalValue().toString());
					}
					attrValue.put(pav.getAttributeName(), sbDB.toString());
					break;
				case 6:
					StringBuffer sbSm = new StringBuffer("");
					if(pav.getBooleanValue() != null){
						sbSm.append(pav.getBooleanValue().toString());
					}
					attrValue.put(pav.getAttributeName(), sbSm.toString());
					break;
				case 7:
					StringBuffer sbDa = new StringBuffer("");
					try {
						if( pav.getDateValue() != null){
							SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							sbDa.append(sf.format(pav.getDateValue()));
						}
					} catch (ParseException e) {
						e.printStackTrace();
					}
					attrValue.put(pav.getAttributeName(), sbDa.toString());
					break;
				case 8:
					attrValue.put(pav.getAttributeName(), pav.getShortTextValue());
					break;
				case 9:
					attrValue.put(pav.getAttributeName(), pav.getShortTextValue());
					break;
				case 10:
					attrValue.put(pav.getAttributeName(), pav.getShortTextValue());
					break;
				case 11:
					attrValue.put(pav.getAttributeName(), pav.getLongTextValue());
					break;
			}
		}
		for(ProductAttGroup group : product.getProductType().getProductAttGroups()){
			for(ProductAttGroupItem item : group.getProductAttGroupItems()){
				Attribute attribute = item.getAttribute();
				if(attribute.getStatus()!=null && FLAG_TRUE.equals(attribute.getStatus())){
					if(listHeader.contains("属性:"+attribute.getAttributeName())){
						ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("属性:"+attribute.getAttributeName()),attrValue.get(attribute.getAttributeName()));
					} else {
						listHeader.add("属性:"+attribute.getAttributeName());
						ExcelUtils.setCellStringValue(row,(short)listHeader.indexOf("属性:"+attribute.getAttributeName()),attrValue.get(attribute.getAttributeName()));
					}
				}
			}
		}
		return null;
	}

}
